package com.three.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.three.entity.Clazz;
import com.three.service.ClazzService;

@Controller
@RequestMapping("/clazz")
public class ClazzController {

	@Autowired
	ClazzService clazzService;
	
	/**
	 * 找到用户所有的班级
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{id}")
	@ResponseBody
	public List<Clazz> findClassesByUid(
			@PathVariable("id") Integer id){
		List<Clazz> classes = new ArrayList<Clazz>();
		classes = clazzService.findClassesByUid(id);
		return classes;
	}
	
	/**
	 * 创建班级
	 * @param clazzName
	 * @param uid
	 * @param tecName
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="createClazz")
	@ResponseBody
	public void createClazz(String clazzName,
			Integer uid,
			String tecName,
			HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out  = response.getWriter();
		JSONObject obj = clazzService.createClazz(clazzName, uid, tecName);
		out.write(obj.toString());
		out.close();
	}
	
	/**
	 * 重置邀请码
	 * @param cid
	 * @param out
	 * 成功则返回重置后的邀请码
	 * 失败返回"failed"
	 */
	@RequestMapping("resetInviteCode")
	@ResponseBody
	public void resetInviteCode(Integer cid,
			PrintWriter out){
		String result = clazzService.resetInviteCode(cid);
		out.write(result);
		out.close();
	}
	
	/**
	 * 停用邀请码
	 * @param cid
	 * @param out
	 */
	@RequestMapping("stopInviteCode")
	@ResponseBody
	public void stopInviteCode(Integer cid,
			PrintWriter out){
		String result = clazzService.stopInviteCode(cid);
		out.write(result);
		out.close();
	}
	
	/**
	 * 更改班级名称
	 * @param cid
	 * @param name
	 * @param out
	 */
	@RequestMapping("updateClazz")
	@ResponseBody
	public void updateClazz(Integer cid,
			String name,
			PrintWriter out){
		String result = clazzService.updateClazz(cid,name);
		out.write(result);
		out.close();
	}
	
	/**
	 * 删除班级
	 * @param cid
	 * @param out
	 */
	@RequestMapping("deleteClazz")
	@ResponseBody
	public void deleteClazz(Integer cid,
			PrintWriter out){
		String result = clazzService.deleteClazz(cid);
		out.write(result);
		out.close();
	}
	
	/**
	 * 加入班级
	 * @param uid
	 * @param inviteCode
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("joinClazz")
	@ResponseBody
	public void joinClazz(Integer uid,
			String inviteCode,
			HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out  = response.getWriter();
		JSONObject obj = clazzService.joinClazz(uid,
				inviteCode);
		out.write(obj.toString());
		out.close();
	}
	
	/**
	 * 退出班级
	 * @param uid
	 * @param cid
	 * @param out
	 */
	@RequestMapping("quitClazz")
	@ResponseBody
	public void quitClazz(Integer uid,
			Integer cid,
			PrintWriter out){
		String result = clazzService.quitClazz(uid,cid);
		out.write(result);
		out.close();
	}
	
}
